home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / gnumake.zip / COMMANDS.C < prev    next >
C/C++ Source or Header  |  1994-04-22  |  12KB  |  487 lines

  1. /* Command processing for GNU Make.
  2. Copyright (C) 1988, 1989, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "dep.h"
  21. #include "commands.h"
  22. #include "file.h"
  23. #include "variable.h"
  24. #include "job.h"
  25.  
  26. extern int remote_kill ();
  27.  
  28. #ifndef    HAVE_UNISTD_H
  29. extern int getpid ();
  30. #endif
  31.  
  32. /* Set FILE's automatic variables up.  */
  33.  
  34. static void
  35. set_file_variables (file)
  36.      register struct file *file;
  37. {
  38.   register char *p;
  39.   char *at, *percent, *star, *less;
  40.  
  41. #ifndef    NO_ARCHIVES
  42.   /* If the target is an archive member `lib(member)',
  43.      then $@ is `lib' and $% is `member'.  */
  44.  
  45.   if (ar_name (file->name))
  46.     {
  47.       unsigned int len;
  48.       p = index (file->name, '(');
  49.       at = (char *) alloca (p - file->name + 1);
  50.       bcopy (file->name, at, p - file->name);
  51.       at[p - file->name] = '\0';
  52.       len = strlen (p + 1);
  53.       percent = (char *) alloca (len);
  54.       bcopy (p + 1, percent, len - 1);
  55.       percent[len - 1] = '\0';
  56.     }
  57.   else
  58. #endif    /* NO_ARCHIVES.  */
  59.     {
  60.       at = file->name;
  61.       percent = "";
  62.     }
  63.  
  64.   /* $* is the stem from an implicit or static pattern rule.  */
  65.   if (file->stem == 0)
  66.     {
  67.       /* In Unix make, $* is set to the target name with
  68.      any suffix in the .SUFFIXES list stripped off for
  69.      explicit rules.  We store this in the `stem' member.  */
  70.       register struct dep *d;
  71.       char *name;
  72.       unsigned int len;
  73.  
  74. #ifndef    NO_ARCHIVES
  75.       if (ar_name (file->name))
  76.     {
  77.       name = index (file->name, '(') + 1;
  78.       len = strlen (name) - 1;
  79.     }
  80.       else
  81. #endif
  82.     {
  83.       name = file->name;
  84.       len = strlen (name);
  85.     }
  86.  
  87.       for (d = enter_file (".SUFFIXES")->deps; d != 0; d = d->next)
  88.     {
  89.       unsigned int slen = strlen (dep_name (d));
  90.       if (len > slen && !strncmp (dep_name (d), name + len - slen, slen))
  91.         {
  92.           file->stem = savestring (name, len - slen);
  93.           break;
  94.         }
  95.     }
  96.       if (d == 0)
  97.     file->stem = "";
  98.     }
  99.   star = file->stem;
  100.  
  101.   /* $< is the first dependency.  */
  102.   less = file->deps != 0 ? dep_name (file->deps) : "";
  103.  
  104.   if (file->cmds == default_file->cmds)
  105.     /* This file got its commands from .DEFAULT.
  106.        In this case $< is the same as $@.  */
  107.     less = at;
  108.  
  109. #define    DEFINE_VARIABLE(name, len, value) \
  110.   (void) define_variable_for_file (name, len, value, o_automatic, 0, file)
  111.  
  112.   /* Define the variables.  */
  113.  
  114.   DEFINE_VARIABLE ("<", 1, less);
  115.   DEFINE_VARIABLE ("*", 1, star);
  116.   DEFINE_VARIABLE ("@", 1, at);
  117.   DEFINE_VARIABLE ("%", 1, percent);
  118.  
  119.   /* Make sure that no dependencies are repeated.  This does not
  120.      really matter for the purpose of updating targets, but it
  121.      might make some names be listed twice for $^ and $?.  */
  122.  
  123.   uniquize_deps (file->deps);
  124.  
  125.   /* Compute the values for $^ and $?.  */
  126.  
  127.   {
  128.     register unsigned int caret_len, qmark_len;
  129.     char *caret_value;
  130.     register char *cp;
  131.     char *qmark_value;
  132.     register char *qp;
  133.     register struct dep *d;
  134.     unsigned int len;
  135.  
  136.     caret_len = qmark_len = 0;
  137.     for (d = file->deps; d != 0; d = d->next)
  138.       {
  139.     register unsigned int i = strlen (dep_name (d)) + 1;
  140.     caret_len += i;
  141.     if (d->changed)
  142.       qmark_len += i;
  143.       }
  144.  
  145.     len = caret_len == 0 ? 1 : caret_len;
  146.     cp = caret_value = (char *) alloca (len);
  147.     len = qmark_len == 0 ? 1 : qmark_len;
  148.     qp = qmark_value = (char *) alloca (len);
  149.  
  150.     for (d = file->deps; d != 0; d = d->next)
  151.       {
  152.     char *c = dep_name (d);
  153.  
  154. #ifndef    NO_ARCHIVES
  155.     if (ar_name (c))
  156.       {
  157.         c = index (c, '(') + 1;
  158.         len = strlen (c) - 1;
  159.       }
  160.     else
  161. #endif
  162.       len = strlen (c);
  163.  
  164.     bcopy (c, cp, len);
  165.     cp += len;
  166.     *cp++ = ' ';
  167.  
  168.     if (d->changed)
  169.       {
  170.         bcopy (c, qp, len);
  171.         qp += len;
  172.         *qp++ = ' ';
  173.       }
  174.       }
  175.  
  176.     /* Kill the last spaces and define the variables.  */
  177.  
  178.     cp[cp > caret_value ? -1 : 0] = '\0';
  179.     DEFINE_VARIABLE ("^", 1, caret_value);
  180.  
  181.     qp[qp > qmark_value ? -1 : 0] = '\0';
  182.     DEFINE_VARIABLE ("?", 1, qmark_value);
  183.   }
  184.  
  185. #undef    DEFINE_VARIABLE
  186. }
  187.  
  188. /* Chop CMDS up into individual command lines if necessary.
  189.    Also set the `lines_flag' and `any_recurse' members.  */
  190.  
  191. void
  192. chop_commands (cmds)
  193.      register struct commands *cmds;
  194. {
  195.   if (cmds != 0 && cmds->command_lines == 0)
  196.     {
  197.       /* Chop CMDS->commands up into lines in CMDS->command_lines.
  198.      Also set the corresponding CMDS->lines_flags elements,
  199.      and the CMDS->any_recurse flag.  */
  200.       register char *p;
  201.       unsigned int nlines, idx;
  202.       char **lines;
  203.  
  204.       nlines = 5;
  205.       lines = (char **) xmalloc (5 * sizeof (char *));
  206.       idx = 0;
  207.       p = cmds->commands;
  208.       while (*p != '\0')
  209.     {
  210.       char *end = p;
  211.     find_end:;
  212.       end = index (end, '\n');
  213.       if (end == 0)
  214.         end = p + strlen (p);
  215.       else if (end > p && end[-1] == '\\')
  216.         {
  217.           int backslash = 1;
  218.           register char *b;
  219.           for (b = end - 2; b >= p && *b == '\\'; --b)
  220.         backslash = !backslash;
  221.           if (backslash)
  222.         {
  223.           ++end;
  224.           goto find_end;
  225.         }
  226.         }
  227.  
  228.       if (idx == nlines)
  229.         {
  230.           nlines += 2;
  231.           lines = (char **) xrealloc ((char *) lines,
  232.                       nlines * sizeof (char *));
  233.         }
  234.       lines[idx++] = savestring (p, end - p);
  235.       p = end;
  236.       if (*p != '\0')
  237.         ++p;
  238.     }
  239.  
  240.       if (idx != nlines)
  241.     {
  242.       nlines = idx;
  243.       lines = (char **) xrealloc ((char *) lines,
  244.                       nlines * sizeof (char *));
  245.     }
  246.  
  247.       cmds->ncommand_lines = nlines;
  248.       cmds->command_lines = lines;
  249.  
  250.       cmds->any_recurse = 0;
  251.       cmds->lines_flags = (char *) xmalloc (nlines);
  252.       for (idx = 0; idx < nlines; ++idx)
  253.     {
  254.       int flags = 0;
  255.  
  256.       for (p = lines[idx];
  257.            isblank (*p) || *p == '-' || *p == '@' || *p == '+';
  258.            ++p)
  259.         switch (*p)
  260.           {
  261.           case '+':
  262.         flags |= COMMANDS_RECURSE;
  263.         break;
  264.           case '@':
  265.         flags |= COMMANDS_SILENT;
  266.         break;
  267.           case '-':
  268.         flags |= COMMANDS_NOERROR;
  269.         break;
  270.           }
  271.       if (!(flags & COMMANDS_RECURSE))
  272.         {
  273.           unsigned int len = strlen (p);
  274.           if (sindex (p, len, "$(MAKE)", 7) != 0
  275.           || sindex (p, len, "${MAKE}", 7) != 0)
  276.         flags |= COMMANDS_RECURSE;
  277.         }
  278.  
  279.       cmds->lines_flags[idx] = flags;
  280.       cmds->any_recurse |= flags & COMMANDS_RECURSE;
  281.     }
  282.     }
  283. }
  284.  
  285. /* Execute the commands to remake FILE.  If they are currently executing,
  286.    return or have already finished executing, just return.  Otherwise,
  287.    fork off a child process to run the first command line in the sequence.  */
  288.  
  289. void
  290. execute_file_commands (file)
  291.      struct file *file;
  292. {
  293.   register char *p;
  294.  
  295.   /* Don't go through all the preparations if
  296.      the commands are nothing but whitespace.  */
  297.  
  298.   for (p = file->cmds->commands; *p != '\0'; ++p)
  299.     if (!isspace (*p) && *p != '-' && *p != '@')
  300.       break;
  301.   if (*p == '\0')
  302.     {
  303.       /* We are all out of commands.
  304.      If we have gotten this far, all the previous commands
  305.      have run successfully, so we have winning update status.  */
  306.       file->update_status = 0;
  307.       notice_finished_file (file);
  308.       return;
  309.     }
  310.  
  311.   /* First set the automatic variables according to this file.  */
  312.  
  313.   initialize_file_variables (file);
  314.  
  315.   set_file_variables (file);
  316.  
  317.   /* Start the commands running.  */
  318.   new_job (file);
  319. }
  320.  
  321. /* This is set while we are inside fatal_error_signal,
  322.    so things can avoid nonreentrant operations.  */
  323.  
  324. int handling_fatal_signal = 0;
  325.  
  326. /* Handle fatal signals.  */
  327.  
  328. RETSIGTYPE
  329. fatal_error_signal (sig)
  330.      int sig;
  331. {
  332.   handling_fatal_signal = 1;
  333.  
  334.   /* Set the handling for this signal to the default.
  335.      It is blocked now while we run this handler.  */
  336.   signal (sig, SIG_DFL);
  337.  
  338.   /* A termination signal won't be sent to the entire
  339.      process group, but it means we want to kill the children.  */
  340.  
  341.   if (sig == SIGTERM)
  342.     {
  343.       register struct child *c;
  344.       for (c = children; c != 0; c = c->next)
  345.     if (!c->remote)
  346.       (void) kill (c->pid, SIGTERM);
  347.     }
  348.  
  349.   /* If we got a signal that means the user
  350.      wanted to kill make, remove pending targets.  */
  351.  
  352.   if (sig == SIGTERM || sig == SIGINT || sig == SIGHUP || sig == SIGQUIT)
  353.     {
  354.       register struct child *c;
  355.  
  356.       /* Remote children won't automatically get signals sent
  357.      to the process group, so we must send them.  */
  358.       for (c = children; c != 0; c = c->next)
  359.     if (c->remote)
  360.       (void) remote_kill (c->pid, sig);
  361.  
  362.       for (c = children; c != 0; c = c->next)
  363.     delete_child_targets (c);
  364.  
  365.       /* Clean up the children.  We don't just use the call below because
  366.      we don't want to print the "Waiting for children" message.  */
  367.       while (job_slots_used > 0)
  368.     reap_children (1, 0);
  369.     }
  370.   else
  371.     /* Wait for our children to die.  */
  372.     while (job_slots_used > 0)
  373.       reap_children (1, 1);
  374.  
  375.   /* Delete any non-precious intermediate files that were made.  */
  376.  
  377.   remove_intermediates (1);
  378.  
  379.   if (sig == SIGQUIT)
  380.     /* We don't want to send ourselves SIGQUIT, because it will
  381.        cause a core dump.  Just exit instead.  */
  382.     exit (1);
  383.  
  384.   /* Signal the same code; this time it will really be fatal.  The signal
  385.      will be unblocked when we return and arrive then to kill us.  */
  386.   if (kill (getpid (), sig) < 0)
  387.     pfatal_with_name ("kill");
  388. }
  389.  
  390. /* Delete FILE unless it's precious or not actually a file (phony),
  391.    and it has changed on disk since we last stat'd it.  */
  392.  
  393. static void
  394. delete_target (file, on_behalf_of)
  395.      struct file *file;
  396.      char *on_behalf_of;
  397. {
  398.   struct stat st;
  399.  
  400.   if (file->precious || file->phony)
  401.     return;
  402.  
  403. #ifndef NO_ARCHIVES
  404.   if (ar_name (file->name))
  405.     {
  406.       if (ar_member_date (file->name) != file->last_mtime)
  407.     {
  408.       if (on_behalf_of)
  409.         error ("*** [%s] Archive member `%s' may be bogus; not deleted",
  410.            on_behalf_of, file->name);
  411.       else
  412.         error ("*** Archive member `%s' may be bogus; not deleted",
  413.            file->name);
  414.     }
  415.       return;
  416.     }
  417. #endif
  418.  
  419.   if (stat (file->name, &st) == 0
  420.       && S_ISREG (st.st_mode)
  421.       && (time_t) st.st_mtime != file->last_mtime)
  422.     {
  423.       if (on_behalf_of)
  424.     error ("*** [%s] Deleting file `%s'", on_behalf_of, file->name);
  425.       else
  426.     error ("*** Deleting file `%s'", file->name);
  427.       if (unlink (file->name) < 0)
  428.     perror_with_name ("unlink: ", file->name);
  429.     }
  430. }
  431.      
  432.  
  433. /* Delete all non-precious targets of CHILD unless they were already deleted.
  434.    Set the flag in CHILD to say they've been deleted.  */
  435.  
  436. void
  437. delete_child_targets (child)
  438.      struct child *child;
  439. {
  440.   struct dep *d;
  441.  
  442.   if (child->deleted)
  443.     return;
  444.  
  445.   /* Delete the target file if it changed.  */
  446.   delete_target (child->file, (char *) 0);
  447.  
  448.   /* Also remove any non-precious targets listed in the `also_make' member.  */
  449.   for (d = child->file->also_make; d != 0; d = d->next)
  450.     delete_target (d->file, child->file->name);
  451.  
  452.   child->deleted = 1;
  453. }
  454.  
  455. /* Print out the commands in CMDS.  */
  456.  
  457. void
  458. print_commands (cmds)
  459.      register struct commands *cmds;
  460. {
  461.   register char *s;
  462.  
  463.   fputs ("#  commands to execute", stdout);
  464.  
  465.   if (cmds->filename == 0)
  466.     puts (" (built-in):");
  467.   else
  468.     printf (" (from `%s', line %u):\n", cmds->filename, cmds->lineno);
  469.  
  470.   s = cmds->commands;
  471.   while (*s != '\0')
  472.     {
  473.       char *end;
  474.  
  475.       while (isspace (*s))
  476.     ++s;
  477.  
  478.       end = index (s, '\n');
  479.       if (end == 0)
  480.     end = s + strlen (s);
  481.  
  482.       printf ("\t%.*s\n", (int) (end - s), s);
  483.  
  484.       s = end;
  485.     }
  486. }
  487.